define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'demand/requirements/index' + location.search,
                    add_url: 'demand/requirements/add',
                    edit_url: 'demand/requirements/edit',
                    del_url: 'demand/requirements/del',
                    multi_url: 'demand/requirements/multi',
                    import_url: 'demand/requirements/import',
                    table: 'demand_center',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area",[ '100%' , '100%']);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"忆群人团建":__('忆群人团建'),"铁血校园素拓":__('铁血校园素拓'),"海速体育":__('海速体育'),"其他":__('其他')}, formatter: Table.api.formatter.normal},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function (form) {


                $(document).on('click','#calculate_cost',function(){
                    var total_price = 0;
                    $('.fee').each(function(){
                        var that = $(this);
                        var price = that.children().eq(1).children().val();
                        var number = that.children().eq(2).children().val();
                        total_price += price*number;
                    })
                    $('#total_fee').val(total_price);
                })


               $(document).on('click','.add1',function(){
                   var that = $(this);
                   var html = that.parents('tr').prop('outerHTML');

                   html = html.replace('<span></span>','&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" class="btn btn-sm btn-success " onclick="func4(this)"><i class="fa fa-minus"></i> 删除</a>');
                   that.parent().parent().parent().append(html);
                   Form.events.datetimepicker(form)
                })

                $(document).on('click','.add2',function(){
                    var number = $('#number').val();
                    number++;
                    $('#number').val(number);
                    var html = $('.moban').children(1).prop('outerHTML');

                    html = html.replace('<span></span>','&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" class="btn btn-sm btn-success " onclick="func4(this)"><i class="fa fa-minus"></i> 删除</a>');
                    html = html.replace('<span2></span2>','&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" class="btn btn-sm btn-success " onclick="func3(this)"><i class="fa fa-minus"></i> 删除</a>');
                    html = html.replace('start_time_mb1','start_time_xc'+number);
                    html = html.replace('end_time_mb1','end_time_xc'+number);
                    html = html.replace('content_mb1','content_xc'+number);
                    html = html.replace('addr_mb1','addr_xc'+number);
                    html = html.replace('remark_mb1','remark_xc'+number);
                    html = html.replace('<div style="text-align: center">1</div>','<div style="text-align: center">'+number+'</div>');
                    $('.dingjian').append(html);
                    Form.events.datetimepicker(form)
                })

                /*$(document).on('click','.add2',function(){
                    number++;
                    var that = $(this);
                    var html = '';
                    html+='<table  data-name="row[test]" >';
                    html+='<tr>';
                    html+='<td>';
                    html+='<table class="table table-responsive fieldlist" data-name="row[test]" style="position:relative" data-template="testtpl" data-tag="tr">';
                    html+='<tr>';
                    html+='<td >';
                    html+='<div style="width:50px;height:50px;border:1px solid #009589;background-color: #009589;color:white;position:absolute;left:-66px;top:43px">';
                    html+='<div style="text-align: center;margin-top: 8px">DAY</div>';
                    html+='<div style="text-align: center">'+number+'</div>';
                    html+='</div>';
                    html+='</td>';
                    html+='</tr>';
                    html+='<tr>';
                    html+='<td>时间</td>';
                    html+='<td>内容安排</td>';
                    html+='<td>地点</td>';
                    html+='<td>备注</td>';
                    html+='<td>操作</td>';
                    html+='</tr>';
                    html+='<tr>';
                    html+='<td>';
                    html+='<input  class=" datetimepicker" data-date-format="HH:mm" data-use-current="true" name="row[start_time_xc'+number+'][]" size="5" type="text" value="">';
                    html+='至';
                    html+='<input  class=" datetimepicker" data-date-format="HH:mm" data-use-current="true" name="row[end_time_xc'+number+']" size="5" type="text" value="">';
                    html+='</td>';
                    html+='<td>';
                    html+='<input  class="form-control" name="row[content_xc'+number+']" type="text">';
                    html+='</td>';
                    html+='<td>';
                    html+='<input  class="form-control" name="row[addr_xc'+number+']" type="text">';
                    html+='</td>';
                    html+='<td>';
                    html+='<input  class="form-control" name="row[remark_xc'+number+']" type="text">';
                    html+='</td>';
                    html+='<td>';
                    html+='<a href="javascript:;" class="btn btn-sm btn-success " onclick="func(this)"><i class="fa fa-plus"></i> 追加</a>';
                    html+='</td>';
                    html+='</tr>';
                    html+='</table>';
                    html+='</td>';
                    html+='</tr>';
                    html+='<tr> <td colspan="5"><a href="javascript:;" class="btn btn-sm btn-success " onclick="func2(this)"><i class="fa fa-plus"></i> 追加</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" class="btn btn-sm btn-success " onclick="func3(this)"><i class="fa fa-minus"></i> 删除</a></td></tr>';
                    html+='</table>';

                    $('.dingjian').append(html);
                     Form.events.datetimepicker(form)
               })*/

                             $('#c-unit_customer_title').on('click',function(){
                                 Fast.api.open(Fast.api.fixurl('customer/unit_customer/index1'),'选择单位',{
                                     area:["60%","70%"],
                                     callback:function(res){
                                         console.log(res);

                                         $("[data-toggle='city-picker']").citypicker('refresh');
                                         var sex_data = res.sex_data;
                                         $('[name="row[sex_data]"]').each(function(){
                                             var that = $(this);

                                             if(that.val()==sex_data)
                                             {
                                                 that.attr('checked','checked')
                                             }
                                             else
                                             {
                                                 that.removeAttr('checked');
                                             }
                                         })


                                         if(res.age_composition!=null)
                                         {
                                             var arr = res.age_composition.split(',');

                                             /*for(var i in arr){
                                                 $('#row[age_composition][]-'+arr[i]).attr('checked','checked');
                                             }*/
                                $('[name="row[age_composition][]"]').each(function(){
                                    var that = $(this);

                                    if(arr.indexOf(that.val())>-1)
                                    {
                                        that.attr('checked','checked');
                                    }
                                    else
                                    {
                                        that.removeAttr('checked');
                                    }
                                })
                            }

                            if(res.participant!=null)
                            {
                                var arr = res.participant.split(',');

                                /*for(var i in arr){
                                    $('#row[age_composition][]-'+arr[i]).attr('checked','checked');
                                }*/
                                $('[name="row[participant][]"]').each(function(){
                                    var that = $(this);

                                    if(arr.indexOf(that.val())>-1)
                                    {
                                        that.attr('checked','checked');
                                    }
                                    else
                                    {
                                        that.removeAttr('checked');
                                    }
                                })
                            }

                            if(res.participant!=null)
                            {
                                var arr = res.activity_goal.split(',');

                                /*for(var i in arr){
                                    $('#row[age_composition][]-'+arr[i]).attr('checked','checked');
                                }*/
                                $('[name="row[activity_goal][]"]').each(function(){
                                    var that = $(this);

                                    if(arr.indexOf(that.val())>-1)
                                    {
                                        that.attr('checked','checked');
                                    }
                                    else
                                    {
                                        that.removeAttr('checked');
                                    }
                                })
                            }

                            //Form.events.citypicker($("form[role=form]"));

                        }
                    })

                })

                $(document).on('click','.types',function () {
                    var that = this
                    var type = $(that).data('type');
                    var search = $(that).data('search');

                    var val = $('#c-'+type+'_type').val();
                    var url = 'category/look';
                    if(search=='所属行业')
                    {
                        url = 'category/industry';
                    }
                    var url = url+'?type=' +  type+'&val='+val+'&search='+search;
                    Fast.api.open(Fast.api.fixurl(url),search,{
                        area:["60%","70%"],
                        callback:function(res){
                            //console.log('res'+res)
                            $(that).val(res)
                            try {
                                if(typeof  callback === 'function'){
                                    callback.call(that,res)
                                }
                            }catch (e) {

                            }
                        }
                    })
                })

                $('#c-teacher_title').on('click',function(){
                    Fast.api.open(Fast.api.fixurl('resource/teacher/index1'),'选择教师',{
                        area:["60%","70%"],
                    })

                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
